package com.allwees.bs.c.module.order.context.bo;

import com.allwees.bs.c.module.order.chain.create.request.OrderCreateRequest;
import com.allwees.bs.c.module.order.dto.CartDTO;
import com.allwees.core.common.util.MoneyUtil;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
@NoArgsConstructor
public class CouponComputeBo {

    /**
     * 使用现金
     **/
    private BigDecimal amtCash = BigDecimal.ZERO;

    /**
     * 订单总金额
     **/
    private BigDecimal amt = BigDecimal.ZERO;

    /**
     * 商品总价
     **/
    private BigDecimal amtProduct = BigDecimal.ZERO;

    /** 加价后商品总价 */
    private BigDecimal amtProductAfterAdd ;
    /** 加价后运费总价 */
    private BigDecimal amtShippingAfterAdd ;

    /**
     * 商品总价
     **/
    private BigDecimal originalAmtProduct = BigDecimal.ZERO;

    /**
     * 运费
     **/
    private BigDecimal amtShipping = BigDecimal.ZERO;

    /**
     * 商品优惠金额
     **/
    private BigDecimal amtProductCoupon = BigDecimal.ZERO;

    /** 优惠总额 */
    private BigDecimal amtDiscount = BigDecimal.ZERO;

    /** 运费优惠金额 */
    private BigDecimal amtShippingDiscount = BigDecimal.ZERO;

    /** 营业税 */
    private BigDecimal originalAmtSaleTax  = BigDecimal.ZERO;

    /** 营业税 */
    private BigDecimal amtSaleTax  = BigDecimal.ZERO;

    /**加价后的营业税 */
    private BigDecimal amtSaleTaxAfterAdd  = BigDecimal.ZERO;

    private BigDecimal deductMore = BigDecimal.ZERO;

    private BigDecimal discountRate = BigDecimal.ZERO;

    private List<CouponComputeItemBo> items;

    private String userUuid;

    public CouponComputeBo(CartDTO cart) {
        this.amtCash = cart.getAmtCash();

        this.amt = cart.getAmt();
        this.amtProduct = cart.getAmtProduct();
        this.amtShipping = cart.getAmtShipping();
        this.amtSaleTax = cart.getAmtSaleTax();//实付税费
        //this.amtProductAfterAdd = cart.getAmtProduct();
        //this.amtShippingAfterAdd = this.amtShipping ;//加价后运费总价
        this.originalAmtProduct = cart.getOriginalAmtProduct();
        this.discountRate = cart.getDiscountRate();
        this.userUuid = cart.getUserUuid();
    }

    public CouponComputeBo(OrderCreateRequest order) {
        this.amtCash = order.getAmtCash();//实付Cash

        this.amt = order.getAmt();//实付总金额
        this.amtProduct = order.getAmtProduct();//实付商品金额
        this.amtShipping = order.getAmtShipping();//实付运费金额
        this.amtSaleTax = order.getAmtSaleTax();//实付税费
        //this.amtProductAfterAdd = order.getAmtProductAfterAdd();//加价后商品总价
        //this.amtShippingAfterAdd = order.getAmtShippingAfterAdd();//加价后运费总价
        //this.originalAmtProduct = order.getOriginalAmtProduct();//原始商品总价
        //this.originalAmtSaleTax = order.getOriginalAmtSaleTax();//原始营业税
        this.amtSaleTaxAfterAdd = order.getAmtSaleTaxAfterAdd();//加价后的营业税
        this.discountRate = order.getDiscountRate();
        this.userUuid = order.getUserUuid();
    }

    public CouponComputeBo computeAmt() {
        this.setAmt(MoneyUtil.add(getAmtProduct(), getAmtShipping(), getAmtSaleTax()));
        return this;
    }

    public CouponComputeBo setItems(List<CouponComputeItemBo> items) {
        this.items = items;
        return this;
    }


    public void rest(CartDTO cart) {
        cart.setAmtProduct(this.amtProduct);
        cart.setAmtSaleTax(this.amtSaleTaxAfterAdd);
        cart.setAmtShipping(this.amtShipping);
        cart.setAmt(this.amt);
        cart.setAmtCash(this.amtCash);
        cart.setAmtProductCoupon(this.amtProductCoupon);//商品优惠
        cart.setDeductMore(this.deductMore);
        Map<String, CouponComputeItemBo> itemMap = getItemMap();
        cart.getItemsWithStock().forEach(item -> itemMap.get(item.getSku().getUuid()).rest(item));
    }


    public void rest(OrderCreateRequest order) {

        order.setAmtProduct(this.amtProduct);
        // order.setAmtProductAfterAdd(this.amtProductAfterAdd);
        // order.setAmtShippingAfterAdd(this.amtShippingAfterAdd);
        //order.setOriginalAmtSaleTax(this.originalAmtSaleTax);
        order.setAmtSaleTaxAfterAdd(this.amtSaleTaxAfterAdd);
        order.setAmtShipping(this.amtShipping);
        order.setAmt(this.amt);
        order.setAmtCash(this.amtCash);
        order.setAmtProductCoupon(this.amtProductCoupon);//商品优惠
        order.setAmtProductDiscount(this.amtProductCoupon);//商品优惠
        order.setAmtShippingDiscount(this.amtShippingDiscount);//运费优惠
        order.setAmtDiscount(MoneyUtil.add(this.amtProductCoupon,this.amtShippingDiscount,this.amtCash));//总优惠
        Map<String, CouponComputeItemBo> itemMap = getItemMap();
        order.getItems().forEach(item -> itemMap.get(item.getSkuUuid()).rest(item));
    }

    private Map<String, CouponComputeItemBo> getItemMap() {
        return items.stream().collect(Collectors.toMap(CouponComputeItemBo::getKey, o -> o));
    }
}
